package com.asia5b.a5bmall.same.dialog

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.Gravity
import android.view.WindowManager
import com.asia5b.a5bmall.same.R
import com.asia5b.a5mall.middleware.base.BaseApplication
import com.asia5b.a5mall.middleware.config.A5BMallConfigCenter
import com.asia5b.a5mall.middleware.base.BaseDialog
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.utils.EcodeHelper
import com.bumptech.glide.Glide
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.dialog_qcode.*

/**
 * Created by zhangyuncai on 2020/1/15.
 */
class QCodeDilaog(val shopModel: AbsShopModel, context: Context) : BaseDialog(context) {
    override val layoutId: Int = R.layout.dialog_qcode
    override val heightStyle: Int = WindowManager.LayoutParams.WRAP_CONTENT
    override val widthPercent: Double = 0.6
    override val gravity: Int = Gravity.CENTER

    override fun initView() {
        val observable = Observable.create<Bitmap> { observableEmitter ->
//            val logo = BitmapFactory.decodeResource(BaseApplication.sApplication.resources,  R.mipmap.ic_launcher)
            val logo = Glide.with(BaseApplication.sContext).asBitmap().load(shopModel.absShopLogo).submit().get();
            val qrCode = EcodeHelper().createQRImage(A5BMallConfigCenter.INSTANCE.WAP_SELLER_URL + "${shopModel.absShopId}", 200, logo)
            if (qrCode != null) {
                observableEmitter.onNext(qrCode)
            } else {
                observableEmitter.onError(Throwable(""))
            }
        }
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    iv_qcode_img.setImageBitmap(it)
                }, Consumer {

                })
    }

    override fun initEvent() {
    }


}